﻿using IDal;
using MD5Hash;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dal
{
    public class UserDal:IUserDal
    {
        MyDbContext db;
        public UserDal(MyDbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="usrInfo">用户表</param>
        /// <returns>返回受影响行数</returns>
        public int AddUser(UsrInfo usrInfo)
        {
           try
            {
                usrInfo.Upwd = usrInfo.Upwd.GetMD5();
                db.UsrInfos.Add(usrInfo);
                return db.SaveChanges();
            }
            catch(Exception ex)
            {
                throw;
            }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="name">姓名</param>
        /// <param name="pwd">密码</param>
        /// <returns>返回受影响值</returns>
        public int Login(string name,string pwd)
        {
            IQueryable<UsrInfo> list = db.UsrInfos.Where(x => x.Uname.Equals(name));
            if(list.Count() > 0)
            {
                pwd = pwd.GetMD5();
                list = db.UsrInfos.Where(x => x.Upwd.Equals(pwd));
                if(list.Count() > 0)
                {
                    return 2;//登录成功
                }
                return 3;//密码错误
            }
            return 4;//账户错误
        }
    }
}
